package com.addr.dao;

import com.addr.entity.Addr;
import com.administrators.entity.Adms;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
@Scope("prototype")
public class AddrDaoImpl implements AddrDao{
    @Autowired
    private SessionFactory sessionFactory =null;
    public boolean add(Addr addr) {
        boolean flag =false;
        Session session = sessionFactory.getCurrentSession();
        session.save(addr);
        flag = true;
        session.getTransaction().commit();
        return flag;
    }

    public boolean delete(Addr addr) {
        boolean flag = false;
        Session session = sessionFactory.getCurrentSession();
        Addr addr1 =session.get(Addr.class,addr.getId());
        session.delete(addr1);
        flag =true;
        session.getTransaction().commit();
        return flag;
    }

    public boolean update(Addr addr) {
        boolean flag = false;
        Session session =sessionFactory.getCurrentSession();
        Addr ss = session.get(Addr.class,addr.getId());
        ss.setId(addr.getId());
        ss.setU_id(addr.getU_id());
        ss.setAddress(addr.getAddress());
        session.update(ss);
        session.getTransaction().commit();
        flag = true;
        return flag;
    }

    public Addr selectOne(Addr addr) {
        List<Addr> addrList = null;
        Session session  = sessionFactory.getCurrentSession();
        org.hibernate.query.Query query = session.createQuery("from Addr u where u.id=ee");
        query.setParameter(0,addr.getId());
        addrList = query.getResultList();
        Addr addr1 = addrList.get(0);
        return addr1;
    }

    public List<Addr> select(Addr addr) {
        List<Addr> addrList = null;
        Session session  = sessionFactory.getCurrentSession();
        org.hibernate.query.Query query = session.createQuery("from Addr");
        addrList = query.getResultList();
        return addrList;
    }
}
